package com.example.securitysample.service;

import jakarta.annotation.Resource;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Collection;

/**
 * @author TonySong
 * @date 2024/5/28 0028
 * @time 17:26
 */
@Service
public class UserServiceImpl  implements UserDetailsService {

    @Resource
    private PasswordEncoder passwordEncoder;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("======  自定义登录逻辑 ========");
        if(!"admin".equalsIgnoreCase(username)) {
            throw new UsernameNotFoundException("对不起，账号不存在！");
        }

        String pwd = this.passwordEncoder.encode("123456");

        return new User(username,pwd,
                AuthorityUtils.commaSeparatedStringToAuthorityList("user,normal"));
    }
}
